<?php

// PHP雪里开轻量级报修系统 V1.0Beta
// 演示地址: http://baoxiu.chalide.cn
// 更新关注: /weivote
// 文件路径: sys/site.php
// 文件大小: 6414 字节
// 最后修改时间: 2025-05-21 15:43:30
// 作者: yujianyue
// 邮件: 15058593138@qq.com
// 版权所有,保留发行权和署名权

/**
 * 本文件功能：系统管理员-系统设置
 * 版权声明：保留发行权和署名权
 * 作者信息：功能反馈:15058593138@qq.com(手机号同微信)
 */

// 处理AJAX请求
if (isset($_GET['act'])) {
    switch ($_GET['act']) {
        case 'save':
            // 保存设置
            $site_name = isset($_POST['site_name']) ? trim($_POST['site_name']) : '';
            $upload_size = isset($_POST['upload_size']) ? intval($_POST['upload_size']) : 5;
            $version = isset($_POST['version']) ? trim($_POST['version']) : '1.0.0';
            
            // 验证数据
            if (empty($site_name)) {
                json_error('请输入网站名称');
            }
            
            if ($upload_size <= 0 || $upload_size > 50) {
                json_error('上传大小限制必须在1-50MB之间');
            }
            
            // 删除旧设置
            $db->delete('site', "1=1");
            
            // 插入新设置
            $settings = [
                ['key' => 'site_name', 'value' => $site_name, 'remark' => '网站名称'],
                ['key' => 'upload_size', 'value' => $upload_size, 'remark' => '上传大小限制(MB)'],
                ['key' => 'version', 'value' => $version, 'remark' => '系统版本']
            ];
            
            foreach ($settings as $setting) {
                $db->insert('site', $setting);
            }
            
            // 生成JSON配置文件
            $config = [
                'site_name' => $site_name,
                'upload_size' => $upload_size,
                'version' => $version
            ];
            
            $json = json_encode($config, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
            file_put_contents('inc/json.php', $json);
            
            json_success('设置保存成功');
            break;
    }
    exit;
}

// 包含头部
include './inc/head.php';

// 获取当前设置
$settings = [];
$result = $db->getAll('site', "1=1");

foreach ($result as $row) {
    $settings[$row['key']] = $row['value'];
}

// 默认值
$site_name = $settings['site_name'] ?? '简易报修系统';
$upload_size = intval($settings['upload_size'] ?? 5);
$version = $settings['version'] ?? '1.0.0';
?>

<div class="page-header">
    <h2 class="page-title">系统设置</h2>
</div>

<div class="card">
    <div class="card-body">
        <form id="settingsForm">
            <div class="form-group">
                <label for="site_name">网站名称</label>
                <input type="text" class="form-control" id="site_name" name="site_name" value="<?php echo htmlspecialchars($site_name); ?>" required>
            </div>
            
            <div class="form-group">
                <label for="upload_size">上传大小限制(MB)</label>
                <input type="number" class="form-control" id="upload_size" name="upload_size" value="<?php echo $upload_size; ?>" min="1" max="50" required>
                <small class="form-text text-muted">图片上传大小限制，单位MB，建议不超过10MB</small>
            </div>
            
            <div class="form-group">
                <label for="version">系统版本</label>
                <input type="text" class="form-control" id="version" name="version" value="<?php echo htmlspecialchars($version); ?>" required>
                <small class="form-text text-muted">更改版本号可以强制客户端刷新缓存</small>
            </div>
            
            <div class="form-group">
                <button type="button" class="btn btn-primary" id="saveBtn">保存设置</button>
            </div>
        </form>
    </div>
</div>

<div class="card mt-4">
    <div class="card-header">
        <h3>系统信息</h3>
    </div>
    <div class="card-body">
        <table class="table">
            <tbody>
                <tr>
                    <th>PHP版本</th>
                    <td><?php echo PHP_VERSION; ?></td>
                </tr>
                <tr>
                    <th>MySQL版本</th>
                    <td><?php echo $mysqli->server_info; ?></td>
                </tr>
                <tr>
                    <th>服务器时间</th>
                    <td><?php echo date('Y-m-d H:i:s'); ?></td>
                </tr>
                <tr>
                    <th>服务器系统</th>
                    <td><?php echo PHP_OS; ?></td>
                </tr>
                <tr>
                    <th>网站目录</th>
                    <td><?php echo realpath('./'); ?></td>
                </tr>
                <tr>
                    <th>上传目录</th>
                    <td><?php echo realpath('./Img'); ?></td>
                </tr>
                <tr>
                    <th>报修记录数</th>
                    <td><?php echo $db->getOne('bxiu', "1=1", "COUNT(*) as count")['count']; ?></td>
                </tr>
                <tr>
                    <th>用户数量</th>
                    <td><?php echo $db->getOne('stux', "1=1", "COUNT(*) as count")['count']; ?></td>
                </tr>
            </tbody>
        </table>
    </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    // 保存设置
    document.getElementById('saveBtn').addEventListener('click', function() {
        var formData = getFormData('settingsForm');
        
        // 简单验证
        if (!formData.site_name) {
            showToast('请输入网站名称', 'warning');
            return;
        }
        
        var uploadSize = parseInt(formData.upload_size);
        if (isNaN(uploadSize) || uploadSize <= 0 || uploadSize > 50) {
            showToast('上传大小限制必须在1-50MB之间', 'warning');
            return;
        }
        
        if (!formData.version) {
            showToast('请输入系统版本', 'warning');
            return;
        }
        
        // 发送请求
        ajax('?do=site&act=save', formData, function(response) {
            if (response.status === 'success') {
                showToast(response.msg, 'success');
                
                // 2秒后刷新页面
                setTimeout(function() {
                    window.location.reload();
                }, 2000);
            } else {
                showToast(response.msg, 'error');
            }
        });
    });
});
</script>
